import request from '../utils/request.js';

/**
 * 封装播鸭请求方法
 * @param {*} url 请求地址
 * @param {*} param 请求参数
 * @param {*} token token
 * @returns
 */
function boyaRequest(url, param, token) {
  const headers = {
    Authorization: `Bearer ${token}`,
  };
  return request(url, {
    headers,
    ...param,
  });
}

// 播鸭自动签到
export const signin = async token => {
  const url = 'https://boya.maiyuan.online/graphql';
  // 签到接口请求参数
  const signinInterface = {
    operationName: 'signIn',
    variables: {},
    query: 'mutation signIn {\n  signIn\n}\n',
  };

  return await boyaRequest(
    url,
    {
      data: signinInterface,
    },
    token
  );
};

// 获取播鸭形象商店数据
export const getAllCommodities = async token => {
  // 域名
  const url = 'https://boya.maiyuan.online/graphql';
  // 形象商店数据接口请求参数
  const commoditieInterface = {
    operationName: 'allCommodities',
    variables: {
      input: {
        type: 2,
        order: {
          price: 0,
        },
      },
    },
    query:
      'query allCommodities($input: allCommoditiesInput!) {\n  allCommodities(input: $input) {\n    totalCount\n    nodes {\n      code\n      image\n      id\n      introduction\n      originalPrice\n      price\n      sales\n      status\n      stock\n      title\n      releaseTime\n      createdAt\n      user {\n        id\n        avatar\n        name\n        __typename\n      }\n      __typename\n    }\n    __typename\n  }\n}\n',
  };
  return await boyaRequest(
    url,
    {
      data: commoditieInterface,
    },
    token
  );
};
